//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * buyguideAction
 * 项目名称:代购系统
 * @author Mengxiangliang
 * 功能描述:
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * @version [V1] 
 */
package com.gamutsoft.amb.buyguide.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;

import com.gamutsoft.amb.base.action.BaseAction;
import com.gamutsoft.amb.buyguide.model.Buyguide;
import com.gamutsoft.amb.buyguide.service.IBuyguideService;
import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.exception.BusinessException;


@SuppressWarnings({"serial","rawtypes"})
public class BuyguideAction extends BaseAction {
	
	private Buyguide buyguide;
	
	private List<Map> buyguideList;
	
	
	private IBuyguideService buyguideService;
	
	private Map<String,List<Map>> showMap;

	/**
	 * 初始化查询Buyguide信息
	 * @author Mengxiangliang
	 * @return
	 */
	public String searchBuyguide()throws BusinessException, Exception {
		try {
		    buyguideList = buyguideService.searchBuyguideListByWhere(new QueryExample(), buyguide);
		    dealMap(buyguideList);
		}catch(Exception e){
			log.error(e);
			throw e;
		}
		return "search_buyguide_success";
	}
	
    private void dealMap(List<Map> list){
    	showMap=new LinkedMap();
    	if(list!=null && list.size()!=0){
    		//保存父节点
    		for(Map map:list){
    			String parent_id=mapForStr(map,"parent_id");
    			String name=mapForStr(map,"name");
    			if("0".equals(parent_id) && !showMap.containsKey(name)){
    				showMap.put(name, new ArrayList());
    			}
    		}
    		//保存子节点
    		for(Map map:list){
    			String parent_id=mapForStr(map,"parent_id");
    			String name=mapForStr(map,"name");
    			if(!"0".equals(parent_id) && !showMap.containsKey(name)){
    				String parentName=mapForStr(map,"parent_name");
    				showMap.get(parentName).add(map);
    			}
    		}
    	}
    }
    private String mapForStr(Map map,String key){
    	String result="";
    	if(map!=null){
    		if(map.get(key)!=null){
    			result=map.get(key).toString();
    		}
    	}
    	return result;
    }
	/**
	 * @return the buyguide
	 */
	public Buyguide getBuyguide() {
		return buyguide;
	}

	/**
	 * @param buyguide the buyguide to set
	 */
	public void setBuyguide(Buyguide buyguide) {
		this.buyguide = buyguide;
	}

	/**
	 * @return the buyguideList
	 */
	public List<Map> getBuyguideList() {
		return buyguideList;
	}

	/**
	 * @param buyguideList the buyguideList to set
	 */
	public void setBuyguideList(List<Map> buyguideList) {
		this.buyguideList = buyguideList;
	}

	/**
	 * @param buyguideService the buyguideService to set
	 */
	public void setBuyguideService(IBuyguideService buyguideService) {
		this.buyguideService = buyguideService;
	}

	public Map<String, List<Map>> getShowMap() {
		return showMap;
	}

	public void setShowMap(Map<String, List<Map>> showMap) {
		this.showMap = showMap;
	}
	
}
